#include"config.h"
#define MATRIX_RCLK (0x01<<20)
#define MATRIX_SRCLK (0x01<<17)
#define MATRIX_SI_X (0x01<<18)
#define MATRIX_SI_Y (0x01<<19)
#define MATRIX_MAT 0xfffffc03
#define MATRIX_NUMBER 0x10
uint16 display_buffer[16]; //缓存数组
uint8 buffer_character_pit = 0x00; //LaticMove函数中用来控制buffer的指针
uint8 character_pit = 0x00;  //LaticMove函数中用来控制要存入字符数组中的单个字符的内容的指针
uint8 display_pit = 0x00;  //Scan函数中用来控制显示的指针
uint16 latic_line_bit = 0x00;  //控制列     
const uint16 font[][16]={0x0002,0x7FF2,0x2812,0x2412,0x2212,0x21FE,0x2012,0x2012,0x2012,0x21FE,0x2212,0x2212,0x2212,0x7FF2,0x0002,0x0000,
0xFFFB,0x807B,0xEDDB,0xEDDB,0x001B,0xEDDB,0xEDDB,0xDDDB,0xDDFB,0xBEEB,0xFEF7,0xC003,0xFEFF,0xFEFF,0x0001,0xFFFF,
0xF9BF,0xC7AF,0xF7B7,0xF7B7,0xF7BF,0x0001,0xF7BF,0xF7B7,0xF1D7,0xC7CF,0x37DF,0xF7AF,0xF66D,0xF7F5,0xD7F9,0xEFFD,
0xFF07,0xC0FF,0xFEFF,0x8001,0xFC3F,0xF2CF,0xCEF1,0x3EFB,0xE00F,0xFDDF,0xFDBF,0xFB03,0xFBFB,0xF7FB,0xEFD7,0xDFEF};

void sendData(uint16 row,uint16 line){
	uint8 tmp;
	IO0CLR|=MATRIX_RCLK ;
	for(tmp=0x00;tmp<16;tmp++)
	{
		IO0CLR |= MATRIX_SRCLK;
        if((row & 0x8000) == 0)  //-----行输入
        {
            IO0CLR |= MATRIX_SI_X;    //发送 低电平
        }        
        else 
        {
            IO0SET |= MATRIX_SI_X;    //发送 高电平
        }
        if((line & 0x8000) == 0) //只有符合目标输入列的列才会使能
        {
            IO0CLR |= MATRIX_SI_Y;    //发送 低电平
        }
        else
        {
            IO0SET |= MATRIX_SI_Y;    //发送 高电平
        }   
        IO0SET |= MATRIX_SRCLK;  //SCK = 1
        row = row << 1;
        line = line << 1;
	}
	IO0SET |= MATRIX_RCLK ;
}

void Init(){
	PINSEL1 &= MATRIX_MAT; //引脚连接模块设置，这里选择P0.17～P0.20作GPIO 
	IO0DIR |= MATRIX_SRCLK; //方向输出 
	IO0DIR |= MATRIX_RCLK; 
	IO0DIR |= MATRIX_SI_X; 
	IO0DIR |= MATRIX_SI_Y; 
}
void Scan(){
    display_pit = display_pit % 16; //使列循环
    latic_line_bit = ~(0x8000 >> display_pit); //控制列
    sendData(display_buffer[display_pit],latic_line_bit); 
    display_pit ++;  
}

void LaticMove(void)
{
    uint8 temp;
    for(temp = 0; temp < 15; temp ++)  //display_buffer数组中的后15个数据向前移一位
    {
        display_buffer[temp] = display_buffer[temp + 1];                
    }
    display_buffer[15] = font[character_pit][buffer_character_pit]; //在第16个数据位上加入新的显示内容
    buffer_character_pit ++;
    if(buffer_character_pit == 16)
    {
        buffer_character_pit = 0;
        character_pit ++;
        if(character_pit == 16)  //显示下一个要显示的字符
        {
            character_pit = 0;
        }
    }
}

uint8 counter = 0;  //控制移动
uint8 judge=0;
void __irq IRQ_Time0Interrupt(){
//    if(counter == 0x8f)
//     {
//         counter = 0;
//         LaticMove(); //移动字符
//     }
    counter ++;
    Scan();  //点阵扫描
    
    T0IR = 0x01; //复位MR0中断
    VICVectAddr=0x00;//清除地址
}

void timer0_init(){
	T0TC=0;
	T0PR=0;
	T0MCR=0x03;
	T0MR0=Fpclk/1000;
	VICIntSelect &= 0xffffffef; 
	VICVectCntl2 =0x20|4;
	VICVectAddr2 =(uint32)IRQ_Time0Interrupt;
	VICIntEnable=(1<<4);
	T0TCR = 0x01;
}


int main(){
	Init();
	timer0_init();
	while(1);
}